package com.kgcx.organization.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.eum.DriverMobileEnum;
import com.kgcx.organization.domain.Driver;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.DriverMobileTerminal;
import com.kgcx.organization.mapper.DriverMobileTerminalMapper;
import com.kgcx.organization.rabbitmq.producer.DriverMobileTerminalProducer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * 驾驶员移动终端服务
 *
 * @author qinze
 */
@Service
public class DriverMobileTerminalService {

    @Resource
    private DriverMobileTerminalMapper driverMobileTerminalMapper;
    @Resource
    private DriverMobileTerminalProducer driverMobileTerminalProducer;

    @Transactional(rollbackFor = Exception.class)
    public void insertDriverMobileTerminal(DriverMobileTerminal driverMobileTerminal) {
        driverMobileTerminal.setCreateTime(new Timestamp(System.currentTimeMillis()));
        driverMobileTerminal.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        driverMobileTerminalMapper.insertSelective(driverMobileTerminal);
        driverMobileTerminalProducer.addDriverMobileTerminalProducer(driverMobileTerminal);
    }

    /**
     * 监管平台推送驾驶员移动端新增信息
     * @param driver
     */
    public void insertByDriver(Driver driver){
        DriverMobileTerminal driverMobileTerminal = new DriverMobileTerminal();
        driverMobileTerminal.setAddress(driver.getAddressCityId().toString());
        driverMobileTerminal.setLicenseId(driver.getLicenseId());
        driverMobileTerminal.setDriverPhone(driver.getPhone());
        driverMobileTerminal.setNetType(Integer.valueOf(driver.getNetType()));
        //app版本号
        driverMobileTerminal.setAppVersion("V1.1");
        driverMobileTerminal.setMapType(DriverMobileEnum.GAODE.getCode());
        driverMobileTerminal.setStatus(DriverMobileEnum.ENABLE.getCode());
        driverMobileTerminal.setCreateTime(new Timestamp(System.currentTimeMillis()));
        driverMobileTerminal.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        driverMobileTerminalProducer.addDriverMobileTerminalProducer(driverMobileTerminal);
    }

     /**
     * 监管平台推送驾驶员移动端修改信息
     * @param driver
     */
    public void updateByDriver(Driver driver){
        DriverMobileTerminal driverMobileTerminal = new DriverMobileTerminal();
        driverMobileTerminal.setAddress(driver.getAddress());
        driverMobileTerminal.setLicenseId(driver.getLicenseId());
        driverMobileTerminal.setDriverPhone(driver.getPhone());
        driverMobileTerminal.setNetType(Integer.valueOf(driver.getNetType()));
        //app版本号
        driverMobileTerminal.setAppVersion("V1.1");
        driverMobileTerminal.setMapType(DriverMobileEnum.GAODE.getCode());
        driverMobileTerminal.setStatus(DriverMobileEnum.ENABLE.getCode());
        driverMobileTerminal.setCreateTime(new Timestamp(System.currentTimeMillis()));
        driverMobileTerminal.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        driverMobileTerminalProducer.updateDriverMobileTerminalProducer(driverMobileTerminal);
    }
    public List<DriverMobileTerminal> selectDriverMobileTerminal(RequestData rd) {
        DriverMobileTerminal driverMobileTerminal = new DriverMobileTerminal();
        driverMobileTerminal.setKid(rd.getLong("kid"));
        return driverMobileTerminalMapper.selectActive(driverMobileTerminal);
    }

    /**
     * 监管平台提供驾驶员终端信息查询
     * @param rd
     * @return
     */
    public List<DriverMobileTerminal> queryDriverMobileWithPage(RequestData rd) {
        DriverMobileTerminal driverMobileTerminal = new DriverMobileTerminal();
        driverMobileTerminal.setKid(rd.getLong("kid"));
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        List<DriverMobileTerminal> list = driverMobileTerminalMapper.selectActive(driverMobileTerminal);
        for (DriverMobileTerminal driverMobile:list){
         //MQ发送消息
         driverMobileTerminalProducer.addDriverMobileTerminalProducer(driverMobile);
        }
        return list;
    }
}
